package com.hxzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.req.ExportIdsReq;
import com.hxzy.common.req.StatusChangeReq;
import com.hxzy.common.util.MarketThreadLocalUtil;
import com.hxzy.controller.admin.dict.req.DictDataAddReq;
import com.hxzy.controller.admin.dict.req.DictDataEditReq;
import com.hxzy.controller.admin.dict.req.DictDataReq;
import com.hxzy.controller.admin.dict.resp.DictDataResp;
import com.hxzy.entity.SysDictData;
import com.hxzy.mapstruct.DictDataMapStruct;
import com.hxzy.service.SysDictDataService;
import com.hxzy.mapper.SysDictDataMapper;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author admin
* @description 针对表【sys_dict_data(字典数据表)】的数据库操作Service实现
* @createDate 2024-02-21 11:30:41
*/
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData>
    implements SysDictDataService{

    /**
     * 根据字典类型查询数据
     * @param dictTypes
     * @return
     */
    @Override
    public Map<String, List<DictDataResp>> searchByDictTypes(String[] dictTypes) {
        List<DictDataResp> dataRespList= this.baseMapper.searchByDictTypes(dictTypes);

        return dataRespList.stream().collect(Collectors.groupingBy(DictDataResp::getDictType));
    }

    @Override
    public List<DictDataResp> search(DictDataReq req) {
        return this.baseMapper.search(req);
    }

    /**
     * 新增
     * @param req
     * @return
     */
    @Override
    public boolean addData(DictDataAddReq req) {
        SysDictData convert = DictDataMapStruct.INSTANCE.convert(req);
        //查询dictType个数=0就设定它为默认值
        LambdaQueryWrapper<SysDictData> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictData::getDictType,req.getDictType());
        long count = this.count(queryWrapper);
        convert.setIsDefault(count==0?"Y":"N");
        convert.setCreateTime(new Date());
        convert.setCreateBy(MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get().getName());
        return this.save(convert);
    }

    /**
     * 修改数据
     * @param req
     * @return
     */
    @Override
    public boolean updateData(DictDataEditReq req) {
        SysDictData db = this.getById(req.getId());
        if(Objects.isNull(db)){
            throw new ServiceException(AckCode.NOT_FOUND_DATA);
        }

        SysDictData convert = DictDataMapStruct.INSTANCE.convert(req);
        convert.setUpdateBy(MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get().getName());
        convert.setUpdateTime(new Date());
        return this.updateById(convert);
    }

    /**
     * 更改状态
     * @param req
     * @return
     */
    @Override
    public boolean changeStatus(StatusChangeReq req) {
        SysDictData db = this.getById(req.getId());
        if(Objects.isNull(db)){
            throw new ServiceException(AckCode.NOT_FOUND_DATA);
        }
        db.setStatus(req.getStatus());
        db.setUpdateTime(new Date());
        db.setUpdateBy(MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get().getName());
        return  this.updateById(db);
    }

    /**
     * 导出选中的数据
     * @param req
     * @return
     */
    @Override
    public List<DictDataResp> exportExcel(ExportIdsReq req) {
        return this.baseMapper.exportExcel(req.getIds());
    }
}




